package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsTypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: GoodsTypeServiceImpl
 * Description:
 * Date: 2022/1/15 22:36
 *
 * @version:
 * @since JDK 1.8
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {


    @Autowired
    private GoodsTypeDao goodsTypeDao;


    @Override
    public List<Map<String,Object>> loadGoodsType(Integer pId) {

        List<GoodsType> goodsTypeList = goodsTypeDao.loadGoodsType(pId);



        if(goodsTypeList == null){
            return null;
        }

        ArrayList<Map<String,Object>> objects = new ArrayList<>();

        for (GoodsType goodsType : goodsTypeList) {
            HashMap map = new HashMap<String,Object>();
            map.put("id",goodsType.getGoodsTypeId());
            map.put("text",goodsType.getGoodsTypeName());
            map.put("iconCls","goods-type");
            HashMap map2 = new HashMap<String,Object>();
            map2.put("state",goodsType.getGoodsTypeState());
            map.put("attributes",map2);

            List<Map<String, Object>> mapList = loadGoodsType(goodsType.getGoodsTypeId());
            if(mapList != null){
                map.put("children",mapList);
            }
            objects.add(map);
            /*if(pId == -1){
                Map<String, Object> childrenMap = loadGoodsType(1);
                if(childrenMap == null){
                    continue;
                }else{
                    map.put("children",childrenMap);
                }

            }else{
                Map<String, Object> childrenMap = loadGoodsType(++pId);
                if(childrenMap == null){
                    continue;
                }else{
                    map.put("children",childrenMap);

                }*/
        }
        return objects;
//        return goodsTypeMap;

    }
    @Override
    public void saveGoodsType(String goodsTypeName, Integer pId) {
        goodsTypeDao.saveGoodsType(goodsTypeName,pId);
    }



    @Override
    public void deleteGoodsType(Integer goodsTypeId) {
        goodsTypeDao.deleteGoodsType(goodsTypeId);
    }
}
